
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "UI.h"
#include "Global.h"

using std::cout;
using std::endl;

UI * UI::instance = NULL;

UI * UI::GetUI()
{
    if(!instance){
#if defined(GRAPHIC)
        instance = new GraphicUI();
#else
        instance = new TextUI();
#endif
    }
    return instance;
}

void UI::DestroyUI()
{
    if(instance){
        delete instance;
        instance = NULL;
    }
}


short UI::GetUserInput()
{	
	char str[1];
	str[0] = getch();	
	return atoi(str);
}

short UI::GetUserInput(short nMin, short nMax )
{
	short n = GetUserInput();

	while( n < nMin || n > nMax )
		n = GetUserInput();

	return n;
}

TextUI::TextUI()
{
    std::cout << "Use console text as User Interface." << std::endl;
}

/* 暂停游戏，等用户输入 */
void TextUI::SystemPause()
{	
	getch();
    cout << endl;
}

/* 系统信息输出 */
void TextUI::SystemOut(const char * stOutString )
{
    // MINGW will predefine _WIN32
#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, SYSTEM_COLOR|0);
	cout << stOutString << endl << endl;
#else
    printf(SYSTEM_STRING, stOutString);
    cout <<endl << endl;
#endif
}

/* 提示信息输出 */
void TextUI::SystemHint(const char * stHintString )
{
    cout << endl;
#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, SYSTEM_HINT_COLOR|0);
	cout << stHintString << endl << endl;
#else
    printf(SYSTEM_HINT_STRING, stHintString);
    cout << endl << endl;
#endif
}

/* 警告信息输出，并暂停 */
void TextUI::SystemWarning(const char * stWarningString )
{
#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, WARNING_COLOR|0);
	cout << stWarningString << endl;
#else
    printf(WARNING_STRING, stWarningString);
    cout << endl;
#endif
	SystemPause();
}

